//
//  CellForHuaY.m
//  HYHoney
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CellForHuaY.h"
#import "UIImageView+WebCache.h"
#import "ModelOfImg.h"
#import "CellOfSelect.h"
#import "NetworkHandeler.h"
#import "DKNightVersion.h"
@interface CellForHuaY ()<NetworkHandelerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain)UICollectionView *collectionHuaY;
@property (nonatomic, retain)NSMutableArray *arr;

@end

@implementation CellForHuaY

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self handle];
        [self createCollectionView];
    }
    return self;
}


- (void)dealloc{

    [_collection release];
    [_flowLayout release];
    [_collectionHuaY release];
    [_arr release];
    [super dealloc];
}

- (void)handle{
    
    NSString *str = @"http://www.jiajudaren.com/index.php?m=api&a=getitems&fid=762&p=1&pg=50";
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
}

- (void)handelerDidComplete:(id)result{
    
    self.arr = [NSMutableArray array];
    NSArray *array = [result objectForKey:@"data"];
    
    for (NSDictionary *dic in array) {
        
        ModelOfImg *model = [[ModelOfImg alloc] init];
        
        model.idN = [dic objectForKey:@"id"];
        model.title = [dic objectForKey:@"title"];
        
        NSString *str = @"http://www.jiajudaren.com";
        NSMutableArray *mstr = [dic objectForKey:@"simg"];
        model.simg = [NSString stringWithFormat:@"%@%@",str,mstr];        model.price = [dic objectForKey:@"price"];
        
        model.url = [dic objectForKey:@"url"];
        model.height = [dic objectForKey:@"height"];
        model.width = [dic objectForKey:@"width"];
        
        [self.arr addObject:model];
        [model release];
    }
    [self.collectionHuaY reloadData];
}

- (void)createCollectionView{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((self.frame.size.width - 80) / 2.0, ((self.frame.size.width - 80) / 2.0) * 1.618);
    
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.flowLayout.footerReferenceSize = CGSizeMake(0, 70);
    
    self.collectionHuaY = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout: self.flowLayout];
    
    [self.contentView addSubview: self.collectionHuaY];
    
    self.collectionHuaY.dataSource = self;
    self.collectionHuaY.delegate = self;
    
    [self.collectionHuaY registerClass:[CellOfSelect class] forCellWithReuseIdentifier:@"pool5"];
    
    self.collectionHuaY.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);
    [self.contentView addSubview:self.collectionHuaY];
    
    self.collectionHuaY.showsHorizontalScrollIndicator = NO;
    
    [self.collectionHuaY release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfSelect *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool5" forIndexPath:indexPath];
    
    ModelOfImg *model = [self.arr objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    return cell;
}


/** 通过 delegate 回调出去.*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if ([self.delegate respondsToSelector:@selector(didClickSelect:)]) {

        [self.delegate didClickSelect:[self.arr objectAtIndex:indexPath.item]];
    }

}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
}


- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    return layoutAttributes;
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
}


@end
